package com.pactera.asmp.server.restfullapi;

import com.pactera.asmp.server.common.constants.PermissionCode;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.entity.RoleDTO;
import com.pactera.asmp.server.service.PermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/permission")
public class RestfullPermissionApi {

    private static final Logger logger = LoggerFactory.getLogger(RestfullPermissionApi.class);

    @Autowired
    private PermissionService permissionService;

    /**
     * 获取所有菜单
     * @return
     */
    @GetMapping(value = "/all")
    public ResponseResult getAllPerMissionList() {
        logger.info("GET /api/v1/permission/all getAllPerMissionList");
        return permissionService.getAllPerMissionList();
    }

    /**
     * 根据角色Id,获取相应的权限
     * @param roleId
     * @return
     */
    @RequiresPermissions({PermissionCode.ROLE_MANAGEMENT})
    @GetMapping(value = "/getPermsByRoleId")
    public ResponseResult getPermsByRoleId(@RequestParam("roleId") Integer roleId) {
        logger.info("GET /api/v1/permission/getPermsByRoleId");
        return permissionService.getPermsByRoleId(roleId);
    }

    /**
     * 创建、修改角色，并绑定权限
     * @param roleDTO
     * @return
     */
    @RequiresPermissions({PermissionCode.ROLE_MANAGEMENT})
    @PostMapping
    public ResponseResult rolePermission(@RequestBody @Validated RoleDTO roleDTO) {
        logger.info("POST /api/v1/permission");
        return permissionService.rolePermission(roleDTO);
    }

    /**
     * 删除角色和权限
     * @param roleId
     * @return
     */
    @DeleteMapping
    public ResponseResult delete(@RequestParam("roleId") Integer roleId) {
        logger.info("delete /api/v2/permission");
        return permissionService.delete(roleId);
    }
}
